package com.haiqi.modules.map.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.DataGridBean;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.AisShip;
import com.haiqi.common.persistence.entity.AlarmMarker;
import com.haiqi.common.persistence.entity.MobileShip;
import com.haiqi.common.utils.PageUtils;
import com.haiqi.modules.map.service.MapService;
import com.haiqi.modules.map.service.MobileShipService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hythzx on 2016/6/23.
 */
@Controller
@RequestMapping("/manage/map")
@SafeAuthority(menuCode = "monitorLocation")
public class MapController extends BaseController{

    @Autowired
    private MobileShipService cyxShipService;

    @Autowired
    private MapService mapService;


    @RequestMapping("/index")
    public String index(HttpSession session, Model model){
        return "/modules/map/map";
    }

    /**
     * 航次信息报告
     * @param session
     * @param shipid
     * @param model
     * @return
     */
    @RequestMapping("/voyage")
    public String voyage(HttpSession session,String shipid, Model model){
        if(StringUtils.isNotEmpty(shipid)){
            model.addAttribute("shipid",shipid);
        }
        return "/modules/map/window/voyage";
    }

    @RequestMapping("/accident")
    public String accident(HttpSession session,String shipid, Model model){
        if(StringUtils.isNotEmpty(shipid)){
            model.addAttribute("shipid",shipid);
        }
        return "/modules/map/window/accident";
    }

    @RequestMapping("/message")
    public String message(HttpSession session, Model model,String ships){
        if(StringUtils.isNotEmpty(ships)){
            model.addAttribute("ships",ships);
        }
        return "/modules/map/window/message";
    }

    @RequestMapping("/navigate/warning")
    public String navigatWarning(Model model){
        return "/modules/map/window/navigatWarning";
    }




    @RequestMapping("/touchLine")
    public String touchLine(Model model,String shipid,String iemi){
        if(StringUtils.isNotEmpty(shipid)){
            model.addAttribute("shipid",shipid);
        }
        if(StringUtils.isNotEmpty(iemi)){
            model.addAttribute("iemi",iemi);
        }
        return "/modules/map/window/touchLine";
    }




    @RequestMapping("/cyxShip")
    public String cyxShip(){
        return "/modules/map/window/cyxShip";
    }

    @RequestMapping("/waters")
    public String watersManage(){
        return "/modules/map/window/watersManager";
    }

    @RequestMapping("/vts")
    public String alarmMarker(){
        return "/modules/map/window/alarmMarker";
    }

    @RequestMapping("/aisShip")
    public String aisShip(){
        return "/modules/map/window/aisShip";
    }

    @RequestMapping("/serverShipCheck")
    public String serverShipCheck(){
        return "/modules/map/window/serverShipCheck";
    }

    @RequestMapping("/serverPointCheck")
    public String serverPointCheck(){
        return "/modules/map/window/serverPointCheck";
    }

    @RequestMapping("/cyxShip/ajax/list")
    @ResponseBody
    public DataGridBean<MobileShip> cyxShip(MobileShip cyxShip, PageBean page){
        PageUtils.generatePage(page);
        List<MobileShip> dataList = cyxShipService.getDataList(cyxShip);
        PageInfo<MobileShip> pageInfo = new PageInfo<>(dataList);
        DataGridBean<MobileShip> mobileShipDataGridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo, mobileShipDataGridBean);
        return mobileShipDataGridBean;
    }

    @RequestMapping("/aisShip/ajax/list")
    @ResponseBody
    public DataGridBean<AisShip> aisShip(AisShip aisShip, PageBean page){
        PageUtils.generatePage(page);
        List<AisShip> dataList = cyxShipService.aisShipList(aisShip);
        PageInfo<AisShip> pageInfo = new PageInfo<>(dataList);
        DataGridBean<AisShip> mobileShipDataGridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo, mobileShipDataGridBean);
        return mobileShipDataGridBean;
    }

    @RequestMapping("/alertList")
    @ResponseBody
    public String getAlertList(){
        String alertList = mapService.getAlertList();
        return alertList;
    }

    @RequestMapping("/oneVTS")
    @ResponseBody
    public String getOneAlarmMarker(String guid){
        String  entity = mapService.getOneAlarmMarker(guid);
        return entity;
    }

    @RequestMapping("/allAlertList")
    @ResponseBody
    public DataGridBean<AlarmMarker> getAllAlertList(String title,String isValid,PageBean page){
        PageUtils.generatePage(page);

        List<AlarmMarker> list = mapService.getAllAlertList(title,isValid);
        PageInfo<AlarmMarker> pageInfo = new PageInfo<>(list);
        DataGridBean<AlarmMarker> alarmMarkerDataGridBean = new DataGridBean<>();
        PageUtils.convertToEasyuiList(pageInfo,alarmMarkerDataGridBean);
        return alarmMarkerDataGridBean;
    }

    @RequestMapping("/dropAlert")
    @ResponseBody
    public Map dropAlert(String guid){
        Map<String,Boolean> response = new HashMap<>();
        boolean success = mapService.dropAlarmMarker(guid);
        response.put("success",success);
        return  response;
    }

    @RequestMapping("/setAlert")
    @ResponseBody
    public Map setAlertValid(String guid,String state){
        Map<String,Boolean> response = new HashMap<>();
        boolean success = mapService.UpdateAlarmMarkerState(guid,state);
        response.put("success",success);
        return  response;
    }

}
